﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.Helper
{
    /// <summary>
    /// 值类型转换助手类
    /// </summary>
    public class ValueTypeConvertHelper
    {
        /// <summary>
        /// object 转 Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object value, decimal defaultValue = 0)
        {
            decimal result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToDecimal(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 int
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static int ToInt(object value,int defaultValue = 0)
        {
            int result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToInt32((value));
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// double? 转 double
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static double ToDouble(object value, double defaultValue = 0)
        {
            double result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToDouble(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 long
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static long ToLong(object value, long defaultValue = 0)
        {
            long result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToInt64(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 float
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static float ToFloat(object value, float defaultValue = 0)
        {
            float result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToSingle(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static string ToString(object value,string defaultValue = "")
        {
            string result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : value.ToString();
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// bool? 转 bool
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static bool ToBoolean(object value, bool defaultValue = false)
        {
            bool result;
            try
            {
                result = (value == null || value == DBNull.Value) ? default : Convert.ToBoolean(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// DateTime? 转 DateTime
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(object value, DateTime defaultValue = default)
        {            
            DateTime result ;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToDateTime(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 byte
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static byte ToByte(object value, byte defaultValue = default)
        {
            byte result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToByte(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 Char
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static char ToChar(object value, char defaultValue = default)
        {
            char result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToChar(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// object 转 SByte
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns></returns>
        public static sbyte ToSByte(object value, sbyte defaultValue = default)
        {
            sbyte result;
            try
            {
                result = (value == null || value == DBNull.Value) ? defaultValue : Convert.ToSByte(value);
            }
            catch
            {
                result = defaultValue;
            }
            return result;
        }

        /// <summary>
        /// 将一个值类型转换为可为空类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">值</param>
        /// <returns></returns>
        public static T? ToNullable<T>(T value)
            where T : struct
        {
            T? res = new T?(value);
            return res;
        }

        /// <summary>
        /// 将一个对象转换为指定类型的可为空类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">值</param>
        /// <param name="defaultValue">值</param>
        /// <returns></returns>
        public static T? ToNullable<T>(object value)
            where T : struct
        {
            T? res = new T?();
            try
            {
                res = value as T?;
            }
            catch
            {
                res = null;
            }
            return res;
        }

        /// <summary>
        /// 将一个对象转换为指定类型的可为空类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">值</param>
        /// <param name="defaultValue">值</param>
        /// <returns></returns>
        public static T? ToNullable<T>(object value, T defaultValue)
            where T : struct
        {
            T? res = ToNullable<T>(value);
            if (res == null)
            {
                res = default;
            }
            return res;
        }

        /// <summary>
        /// DataRow转为Object类型，DataRow指定值不存在或列名错误，返回默认值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="rowColumnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static object DataRowToObject(DataRow row,string rowColumnName,object defaultValue = null)
        {
            object res = defaultValue;
            try
            {
                res = row[rowColumnName];
            }
            catch
            {

            }
            return res;
        }
    }
}
